package com.shinhye.mobilelottery.ui.manager;

import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Observable;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.RelativeLayout;

import com.shinhye.mobilelottery.R;
import com.shinhye.mobilelottery.ui.BaseUI;
import com.shinhye.mobilelottery.ui.HallUI;
import com.shinhye.mobilelottery.utils.MemoryManager;
import com.shinhye.mobilelottery.utils.PromptManager;
import com.shinhye.mobilelottery.utils.SoftMap;

public class MiddleManager extends Observable{
	private MiddleManager(){}
	private static MiddleManager instance = new MiddleManager();
	public static MiddleManager getInstance(){
		return instance;
	}
	
	private BaseUI currUI;
	private RelativeLayout middle;
	private static Map<String, BaseUI> uiCache;
	// 利用手机的内存空间，换应用的运行速度
	// 如果空间不足了？？？？？
	
	static{
		if(false) {
			uiCache = new HashMap<String, BaseUI>();
		} else {
			uiCache = new SoftMap<String, BaseUI>();
		}
	}
	
	private LinkedList<String> uiList = new LinkedList<String>();
	
	public void setMiddle(RelativeLayout middle) {
		this.middle = middle;
	}
	
	public BaseUI getCurrUI() {
		return currUI;
	}

	@Deprecated
	public void changeUI(BaseUI ui) {
		middle.removeAllViews();
		View child = ui.getView();
		middle.addView(child);
		child.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.ia_view_change));
	}
	
	public void changeUI(Class<? extends BaseUI> clazz, Bundle bundle) {
		if(uiCache.size() != 0 && currUI.getClass() == clazz) {
			return;
		}
		
		if(currUI != null)
			currUI.onPause(); //UI消失时执行
		middle.removeAllViews();
		try {
			BaseUI targetui;
			String sName = clazz.getSimpleName();
			if(uiCache.containsKey(sName)) {
				targetui = uiCache.get(sName);
			} else {
				Constructor<? extends BaseUI> constructor = clazz.getConstructor(Context.class);
				targetui = constructor.newInstance(getContext());
			}
			View child = targetui.getView();
			middle.addView(child);
			child.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.ia_view_change));
			targetui.setBundle(bundle);
			targetui.onResume(); //UI显示时执行
			
			uiCache.put(sName, targetui);
			uiList.push(sName);
			currUI = targetui;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		changeTitleAndBottom();
	}
	
	private void changeTitleAndBottom() {
		setChanged();
		notifyObservers(currUI.getId());
	}

	public boolean goback() {
		if(uiList.size() == 1) {
			return false;
		}
		
		uiList.pop();
		
		String firstUi = uiList.getFirst();
		BaseUI ui = uiCache.get(firstUi);
		
		if(ui != null) {
			currUI.onPause(); //UI消失时执行
			middle.removeAllViews();
			View child = ui.getView();
			middle.addView(child);
			child.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.ia_view_change));
			ui.onResume(); //UI显示时执行
			
			currUI = ui;
			changeTitleAndBottom();
		} else {
			changeUI(HallUI.class, null);
			PromptManager.showToast(getContext(), "当前应用在低内存下运行！");
		}
		return true;
	}
	
	public Context getContext() {
		return middle.getContext();
	}
	
	/**
	 * Clear all UI access history.
	 */
	public void clear() {
		uiList.clear();
	}
}
